
package intention;

import com.aetrion.flickr.*;
import com.aetrion.flickr.photos.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

public class FlickrGrabber {

    private Flickr flickr;
    private PhotoList interestingPhotos;
    private int interestingIterator;
    private BufferedImage nextImage = null;
    private BufferedImage backupImage = null;
    private int defaultSize = 1200;
    private BufferedImage defaultImage;

    public FlickrGrabber() {
        try
        {
            this.defaultImage = ImageIO.read(new File("src\\intention\\images\\splatch.jpg"));
        }
        catch(Exception e)
        {
            //just grey then
        }
        try {
            String apiKey = "6fc282abacbba68fb0be647482c9f179";
            String secret = "37bf0c96534c514a";
            this.flickr = new Flickr(apiKey, secret, new REST());
            this.interestingPhotos = this.flickr.getInterestingnessInterface().getList();
            interestingIterator = 0;
            prepNextImage();
        } catch (Exception e) {
            nextImage = defaultImage;
            backupImage = defaultImage;
        }
    }

    private void prepNextImage() {
        try {
            if (nextImage == null) {
                Photo onePhoto = (Photo) interestingPhotos.get(interestingIterator);
                interestingIterator++;
                nextImage = flickr.getPhotosInterface().getImage(onePhoto, defaultSize);
                onePhoto = (Photo) interestingPhotos.get(interestingIterator);
                interestingIterator++;
                backupImage = flickr.getPhotosInterface().getImage(onePhoto, defaultSize);
                return;
            }
            else
            {
                nextImage = backupImage;
                Photo onePhoto = (Photo) interestingPhotos.get(interestingIterator);
                interestingIterator++;
                backupImage = flickr.getPhotosInterface().getImage(onePhoto, defaultSize);
            }
        } catch (Exception e) {
            nextImage = defaultImage;
            backupImage = defaultImage;
        }
    }

    public BufferedImage grabInterestingImage() {
        prepNextImage();
        return nextImage;
    }
}